﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KC.Common;
using KC.Framework.Tenant;
using KC.Service.Core.Message;
using KC.Service.Core.DTO.Message;
using KC.Service.Core.WebApiService.Business;

namespace KC.Service.Account.Message
{
    public class UserTemplateGenerator : AbstractMessageGenerator
    {
        public static string User_Created = "MGT2016010100001";
        public static string User_ResetPassword = "MGT2016010100002";

        public UserTemplateGenerator(Tenant tenant, string replaceUserId, string messagecode,
            IMessageApiService messageApiService,
            Microsoft.Extensions.Logging.ILogger<UserTemplateGenerator> logger)
            : this(tenant, replaceUserId, messagecode, null, messageApiService, logger)
        {
        }

        public UserTemplateGenerator(Tenant tenant, string replaceUserId, string messagecode,
            Dictionary<string, string> replaceDict,
            IMessageApiService messageApiService,
            Microsoft.Extensions.Logging.ILogger<UserTemplateGenerator> logger)
            : base(tenant, replaceUserId, messagecode, replaceDict, messageApiService, logger)
        {
        }

        public override List<string> GetReplaceParameters()
        {
            var result = new List<string>
            {
                "{TenantName}",
                "{TenantDisplayName}",
                "{AdministratorName}",
                "{UserDisplayName}",
                "{UserAccount}",
                "{UserPassword}",
                "{gurl}"

            };
            return result;
        }

        public override MessageClassDTO GenerateMessageClass()
        {
            var messageClass = GetMessageClassByCode();
           var messageTemplates =messageClass.MessageTemplates.Where(o => !o.IsDeleted && !string.IsNullOrEmpty(o.Content));
            messageClass.MessageTemplates = messageTemplates.ToList();

            if (messageClass == null)
            {
                throw new ArgumentNullException("MessageCode", string.Format("不支持的消息类型（MessageCode={0}）", MessageCode));
            }

            foreach (
                var template in
                    messageClass.MessageTemplates.Where(o => !o.IsDeleted && !string.IsNullOrEmpty(o.Content)))
            {
                if (!ReplaceContent.ContainsKey("{TenantName}"))
                {
                    ReplaceContent.Add("{TenantName}", Tenant.TenantName);
                }
                else
                {
                    ReplaceContent["{TenantName}"] = Tenant.TenantName;
                }

                if (!ReplaceContent.ContainsKey("{TenantDisplayName}"))
                {
                    ReplaceContent.Add("{TenantDisplayName}", Tenant.TenantDisplayName);
                }
                else
                {
                    ReplaceContent["{TenantDisplayName}"] = Tenant.TenantDisplayName;
                }

                if (!ReplaceContent.ContainsKey("{UserDisplayName}"))
                {
                    ReplaceContent.Add("{UserDisplayName}",
                        SerializeHelper.GetValueFromJson(InputParameter, "UserDisplayName"));
                }
                else
                {
                    ReplaceContent["{UserDisplayName}"] = SerializeHelper.GetValueFromJson(InputParameter,
                        "UserDisplayName");
                }

                if (!ReplaceContent.ContainsKey("{AdministratorName}"))
                {
                    ReplaceContent.Add("{AdministratorName}",
                        SerializeHelper.GetValueFromJson(InputParameter, "AdministratorName"));
                }
                else
                {
                    ReplaceContent["{AdministratorName}"] = SerializeHelper.GetValueFromJson(InputParameter,
                        "AdministratorName");
                }

                if (!ReplaceContent.ContainsKey("{UserAccount}"))
                {
                    ReplaceContent.Add("{UserAccount}", SerializeHelper.GetValueFromJson(InputParameter, "UserAccount"));
                }
                else
                {
                    ReplaceContent["{UserAccount}"] = SerializeHelper.GetValueFromJson(InputParameter, "UserAccount");
                }

                if (!ReplaceContent.ContainsKey("{UserPassword}"))
                {
                    ReplaceContent.Add("{UserPassword}",
                        SerializeHelper.GetValueFromJson(InputParameter, "UserPassword"));
                }
                else
                {
                    ReplaceContent["{UserPassword}"] = SerializeHelper.GetValueFromJson(InputParameter, "UserPassword");
                }
                if (!ReplaceContent.ContainsKey("{gurl}"))
                {
                    ReplaceContent.Add("{gurl}",
                        SerializeHelper.GetValueFromJson(InputParameter, "gurl"));
                }
                else
                {
                    ReplaceContent["{gurl}"] = SerializeHelper.GetValueFromJson(InputParameter, "gurl");
                }
                template.Content = ReplaceContentByDict(template.Content);
               
            }

            return messageClass;
        }
    }
}
